package hz.mobileproject.pda.ui;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.annotation.Nullable;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.util.LogUtil;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.push.mqtt.MqttClientMgr;
import hz.mobileproject.pda.receiver.AlarmReceiver;
import hz.mobileproject.pda.service.ReportHearBeatService;
import hz.mobileproject.pda.ui.frgment.MainFragment;
import hz.mobileproject.pda.utils.WindowUtils;
import me.yokeyword.fragmentation.SupportActivity;
import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator;
import me.yokeyword.fragmentation.anim.FragmentAnimator;

public class IndexActivity extends SupportActivity {

    private PushReceiver mPushReceiver = new PushReceiver();
    private PowerManager.WakeLock wakeLock = null;

    class PushReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(MqttClientMgr.ACTION_CONNECTION_LOST)){
            }else if (intent.getAction().equals(MqttClientMgr.ACTION_MESSAGE_ARRIVED)){
                try {
//                    String topWord = "topic/" + SPUtils.getInstance("app_login").getString("department_id") + "/alarm";
                    String msgContent = intent.getStringExtra("content");
                    JSONObject msg = new JSONObject(msgContent);
                    String type = msg.getString("push_type");
                    String push_data = msg.getString("push_data");
                    if("3001".equals(type)) {
                        String push_user = msg.getString("push_user");
                        if (!push_user.equals(MqttClientMgr.CLIENT_ID)){
                            JSONObject jsonObject= new JSONObject(push_data);
                            //上报部门名称
                            String report_dept_name = jsonObject.getString("report_dept_name");
                            //上报民警
                            String report_police_name = jsonObject.getString("report_police_name");
                            //报警类型名称
                            String alarm_type_name = jsonObject.getString("alarm_type_name");
                            //民警报警的罪犯
                            String alram_prisoner = jsonObject.getString("prisoner_name");
                            WindowUtils.showPopupWindow(IndexActivity.this,report_dept_name,report_police_name,alarm_type_name,alram_prisoner);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * 更新手持机信息接口
     */
    @Override
    protected void onResume() {
        super.onResume();
//        try {
//            Intent startIntent = new Intent(this,ReportHearBeatService.class);
//            LogUtil.e("调用心跳接口");
//            startService(startIntent);
//        }catch (Exception e) {
//            LogUtil.e("开启心跳接口失败。。。");
//        }


    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.index_activity);

        try{
            IntentFilter filter = new IntentFilter();
            filter.addAction(MqttClientMgr.ACTION_CONNECTION_LOST);
            filter.addAction(MqttClientMgr.ACTION_MESSAGE_ARRIVED);
            registerReceiver(mPushReceiver,filter);
        }catch (Exception e){
            e.printStackTrace();
        }

        if (findFragment(MainFragment.class) == null) {
            loadRootFragment(R.id.fl_container, MainFragment.newInstance());
        }

        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, IndexActivity.class.getName());
        wakeLock.acquire();
    }

    @Override
    protected void onDestroy() {
        if (null != mPushReceiver){
            unregisterReceiver(mPushReceiver);
        }
        if (wakeLock != null) {
            wakeLock.release();
            wakeLock = null;
        }
        LogUtil.e("aaaaaaaaaaaaaaa");
        Intent intent =new Intent(this, AlarmReceiver.class);
        intent.setAction("repeating");
        PendingIntent sender=PendingIntent
                .getBroadcast(this, 0, intent, 0);
        AlarmManager alarm=(AlarmManager)this.getSystemService(ALARM_SERVICE);
        alarm.cancel(sender);
        super.onDestroy();
    }

    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        // 设置横向(和安卓4.x动画相同)
        return new DefaultHorizontalAnimator();
    }


    private static final long WAIT_TIME = 2000L;
    private long TOUCH_TIME = 0;


}